from vehicleIDM import vehicle
from cs1graphics import *
import sys
import random
#import psyco  for use on other machines
#psyco.full()
from parse import xmlparse
import os

params=xmlparse("params.xml")


FileName= open(os.getcwd()+"\\trafficdata\\"+params.getAttrStr("Filename","FName")+".txt",'w')#Time,Position,Speed# if commented data goes to window
sys.stdout=FileName

Tstep=params.getAttrFloat("timestep", "Tstep")
Tsim=int(params.getAttrFloat("simtime","Tsim")*(1/Tstep)+1)
NumCars=int(params.getAttrFloat("numcars","Ncars"))
TrackCar=range(NumCars)#which car to take data from (eventually take multiple cars data to prosess#standing/ moving waves in traffic patterns
CarCrashPos=100
CarCrashDis=150
CarCrashCar=16
v0=23.2
Traffic=range(NumCars)#Vehicle objects
MovingTraffic=range(NumCars)#Visiual objects
RoadLength=params.getAttrFloat("tracklength", "Tlength")
RoadWidth=RoadLength/3.0
RoadHeight=300
sigma=.15#not used
mu=.9#not used
CrashCount=0
graphics="on"


if graphics=="on":
    paper=Canvas(w=RoadWidth,h=RoadHeight,bgColor='grey',title='Traffic Simulation~Mike Hahn',autoRefresh=False)


for i in Traffic: #start traffic with equal spacing 
    if graphics=="on":
        MovingTraffic[i]=Rectangle(w=7,h=21/5)
        MovingTraffic[i].setFillColor('darkgreen')
        clock=Text('00:00')# new 10-6-09
        clock.moveTo(925,10)
    
    if i in TrackCar:#  data recording
        Traffic[i]=vehicle(float(RoadLength/NumCars)*i,v0,RoadLength,i,TrackCar='On',T_Step=Tstep,Speedlimit=params.getAttrFloat("desVelocity","desVelo"),T=params.getAttrFloat("timemean","TsMean"),a=params.getAttrFloat("maxAccel","a"),b=params.getAttrFloat("desdecel","b"),beta=params.getAttrFloat("delta","accelExp"))
        if graphics=="on":
            MovingTraffic[i].moveTo(Traffic[i].position,50)
            MovingTraffic[i].setFillColor('blue')
            

    else:
        Traffic[i]=vehicle(i*float(RoadLength/NumCars),0,RoadLength,i)
        if graphics=="on":
            MovingTraffic[i].moveTo(Traffic[i].position,50)

    if graphics=="on":
        if i==CarCrashCar:
            MovingTraffic[i].setFillColor('red')
if graphics=="on":
    for j in range(NumCars):
        paper.add(MovingTraffic[j])# adding graphics to window
    paper.add(clock)

for Time in range(Tsim):

    for k in range(NumCars):
       # Traffic[k].step(10000,1000)            
        Traffic[k].step(Traffic[(k+1)%NumCars].position,Traffic[(k+1)%NumCars].speed)
        pos=Traffic[k].position

        if pos>CarCrashPos and pos<(CarCrashPos+CarCrashDis) and k==CarCrashCar and CrashCount<8:
            Traffic[k].setSpeed(10.0)
            CrashCount=CrashCount+1
    if graphics=="on":
        for l in range(NumCars):

            pos=Traffic[l].position

            if pos> RoadWidth*2:
                MovingTraffic[l].moveTo(pos-RoadWidth*2,250)
            elif pos>RoadWidth:
                MovingTraffic[l].moveTo(RoadWidth*2-pos,150)
            else:
                MovingTraffic[l].moveTo(pos,50)
                
        if Time%3==0:
            tnow=float(Time*Tstep)
            clock.setMessage(str(int(tnow/3600))+':'+str(int(tnow/60)%60)+':'+str(int(tnow % 60))+', '+str(int(tnow))+' Seconds')
            paper.refresh()

for l in range(NumCars):
    Traffic[l]=[]
FileName.close()    
